package cn.ecut.servletcontext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @author lujiapeng
 * @className SetDataServlet
 * @description TODO
 * @date 2020/9/11 16:56
 **/
@WebServlet("/setData")
public class SetDataServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、获取到具体的ServletContext
        ServletContext application = this.getServletContext();

        // 2、存放数据
        application.setAttribute("currentDate" , System.currentTimeMillis() );

        // 如果想要获取数据，那么就可以通过 getAttribute 获取
        Object currentDate = application.getAttribute("currentDate");
        System.out.println( currentDate );
        // 获取当前应用的上下文路径
        System.out.println( application.getContextPath() );
        // 通过request 对象，也可以获取到具体的 上下文路径
        System.out.println( request.getContextPath() ); 
    }
}
